package calculatePlus;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalculateFrame extends JFrame implements ActionListener {

    private Container contentPane;
    private JMenuBar header;
    private JMenu curCalcType;
    private JMenuItem otherCalcType;

    private boolean calcStatus;

    private JTextField resultField;
    private JTextField inputField;

    private JPanel textPanel;
    private KeyPanel keysPanel;


    public CalculateFrame() {
        super("计算器");
        setSize(380, 480);
        int x = (int) ((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2 - this.getSize().getWidth() / 2);
        int y = (int) ((int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2 - this.getSize().getHeight() / 2);
        setLocation(x, y);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        drawWindow();
        setVisible(true);
    }

    public void init() {
        calcStatus = false;

        contentPane = getContentPane();

        // 菜单栏初始化
        header = new JMenuBar();
        curCalcType = new JMenu("标准");
        otherCalcType = new JMenuItem("科学");

        textPanel = new JPanel(new GridLayout(2, 1));
        resultField = new JTextField();
        inputField = new JTextField();
    }

    public void keyPanelInit() {
        keysPanel = new KeyPanel(this.calcStatus);
        keysPanel.setInputField(inputField);
        keysPanel.setResultField(resultField);
        contentPane.add(keysPanel, BorderLayout.CENTER);
    }

    public void drawWindow() {
        contentPane.setLayout(new BorderLayout());
        curCalcType.add(otherCalcType);
        header.add(curCalcType);
        setJMenuBar(header);
        otherCalcType.addActionListener(this);
        resultField.setColumns(33);
        inputField.setColumns(33);
        resultField.setHorizontalAlignment(JTextField.RIGHT);
        inputField.setHorizontalAlignment(JTextField.RIGHT);
        textPanel.add(resultField);
        textPanel.add(inputField);
        contentPane.add(textPanel, BorderLayout.NORTH);

        keyPanelInit();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!calcStatus) {
            curCalcType.setText("科学");
            otherCalcType.setText("标准");

        } else {
            curCalcType.setText("标准");
            otherCalcType.setText("科学");
        }
        calcStatus = !calcStatus;

        contentPane.remove(keysPanel);
        keyPanelInit();
    }
}
